require("./index.scss");
var Pagination = require("COMMON/modules/pagination-x/v1.0");
var ParseTemplate = ZFT.Util.ParseTemplate;
var Template = {
    editNotice: ParseTemplate(require("./tpl/editnotice.xtpl")),
    addnote: ParseTemplate(require("./tpl/addnotice.xtpl")),
    noticeList: ParseTemplate(require("./tpl/noticelist.xtpl")),
    delNotice:ParseTemplate(require("./tpl/delnotice.xtpl"))
};
var Main = ZFT.Util.Class({
    container: $("body"),
    init: function () {
        var _this = this;
        var pagination = this.pagination = new Pagination({
            container: $(".paginationWrap"),
            count: 5,
            showTotal: true,
            jump: true,
            filter: true
        });
        pagination.on("page.switch", function (toPage, currentPage, totalPage) {
            pagination.render({ current: currentPage, toPage: toPage }),
                _this.getTplList(toPage);
        })
        _this.getTplList(1);
        $("#schNotice").on("click", function () {
            _this.getTplList(1);
        });
        $("#addTem").on("click",function(){
            _this.showAddTemModal();
        })
    },

    //显示新增模板模态框
    showAddTemModal: function () {
        var _this = this;
        var html = Template.addnote();
        $("#modal").html(html);
        _this.closeModal();
        _this.toggleNoticeBox();
        _this.saveAddTpl("/r/Message_add", "a");
    },
    closeModal: function () {
        $(".cancel,.close").on("click", function (e) {
            var tarBtn = $(e.currentTarget);
            tarBtn.parents(".m-modal_bg").fadeOut(200);
        });
    },
    //切换通知事件
    toggleNoticeBox: function () {
        $("#evType").on("change", function (e) {
            var tarOpt = $(e.currentTarget);
            var tarVal = tarOpt.val();
            var ev1='<option value="1">付款方</option>';
            var ev2='<option value="1">付款方</option><option value="2">收款方</option>';
            if (tarVal == 1) {
                $(".u-notice_box:eq(0)").css("display", "inline-block").siblings(".u-notice_box").hide().find("input").prop("checked", false);
                $("#member").html(ev1);
            } else if (tarVal == 2) {
                $(".u-notice_box:eq(1)").css("display", "inline-block").siblings(".u-notice_box").hide().find("input").prop("checked", false);
                $("#member").html(ev2);
            }
        })
    },
    //保存新增或编辑模板
    saveAddTpl: function (url, type, id) {
        //url:接口地址   type: a->添加 e->编辑   id :编辑时要传的参数
        var _this = this;
        $("#saveTpl").on("click", function (e) {
            var reg = /^[1-9]\d*$/;
            var arr = [];
            var evType = (type == "a") ? $("#evType").val() : $("#evType").attr("data-evtype");
            var notify_day = $.trim($("#notifyDay").val());
            var param = {
                title: $.trim($("#title").val()),
                contract_type: $("#ctType").val(),
                send_type: $("#sendType").val(),
                notify_method: $("#notifyMethod").val(),
            }
            var noticeTem = $("#noticeBox input:checked");
            if (type == "a") {
                param["event_type"] = evType;
                param["message_type"] = $("#msgType").val();
                param["contract_type"] = $("#ctType").val();
                param["member"] = $("#member").val();
            }
            if (type == "e" && id) param["id"] = id
            for (var i = 0, len = noticeTem.length; i < len; i++) {
                var val = noticeTem.eq(i).val();
                arr.push(val);
            };
            if (param["title"].length < 1) return ZFT.Util.STip("fail", "请输入标题!", 4000);
            if (evType == 1) {
                if (!reg.test(notify_day)) {
                    return ZFT.Util.STip("fail", "请输入正确的通知天数!", 4000);
                } else {
                    param["notify_day"] = notify_day
                }
            }

            if (arr.length < 1) return ZFT.Util.STip("fail", "请至少勾选一个模板内容!", 4000);
            param["content"] = JSON.stringify(arr);
            ZFT.Util.Ajax(url, {
                type: "POST",
                dataType: "json",
                params: param,
                success: function (res) {
                    if (res.code == 200) {
                        $("#modal").html("");
                        ZFT.Util.STip("success", res.msg, 4000);
                        _this.getTplList(1);
                    } else if (res.code == 102) {
                        return ZFT.Util.STip("fail", "未登录,请重新登录", 4000, function () {
                            window.location.href = "/index/index/login";
                        });
                    } else {
                        return ZFT.Util.STip("fail", res.msg);
                    }
                }
            })
        });
    },

    //获取消息列表
    getTplList: function (toPage) {
        var _this = this;
        var pagination = $(".paginationWrap");
        var page_size = pagination.attr("data-size");
        var param = {
            title: $.trim($("#titleInp").val()),
            event_type: $("#eventType").val(),
            message_type: $("#messageType").val(),
            page: toPage ? toPage : 1,
            page_size: page_size ? page_size : 15
        }
        var tbody = $("#tbody");
        ZFT.Util.Ajax("/r/Message_search", {
            type: "POST",
            dataType: "json",
            params: param,
            loading: function () {
                var loading = "<tr><td colspan='8'>正在加载,请稍后...</td></tr>"
                pagination.hide();
                tbody.html(loading);
            },
            success: function (res) {
                var data = res.data.list;
                if (res.code == 200) {
                    var html = Template.noticeList({ data: data });
                    tbody.html(html);
                    _this.pagination.render({ current: toPage, toPage: toPage, total: res.data.total })
                    _this.operateNotice();
                } else if (res.code == 102) {
                    tbody.html('<tr><td colspan="8"><a href="/index/index/login">' + res.msg + '</td></tr>');
                } else {
                    tbody.html('<tr><td colspan="8">' + res.msg + '</td></tr>');
                }
            }
        })
    },
    //查看消息详情或删除消息模板
    operateNotice: function () {
        var _this = this;
        $("#tbody .btn-view").on("click", function (e) {
            var tarBtn = $(e.currentTarget);
            var id = tarBtn.attr("data-id");
            _this.viewNotice(id);
        })
        $("#tbody .btn-del").on("click", function (e) {
            var tarBtn = $(e.currentTarget);
            var id = tarBtn.attr("data-id");
            _this.delNotice(id);
        })
    },
    //查看消息
    viewNotice: function (id) {
        var _this = this;
        ZFT.Util.Ajax("/r/Message_detail", {
            type: "POST",
            dataType: "json",
            params: {
                id: id
            },
            success: function (res) {
                if (res.code == 200) {
                    var html = Template.editNotice({ data: res.data });
                    $("#modal").html(html);
                    _this.closeModal();
                    _this.saveAddTpl("/r/Message_edit", "e", id);
                } else if (res.code == 102) {
                    return ZFT.Util.STip("fail", "未登录,请重新登录", 4000, function () {
                        window.location.href = "/index/index/login";
                    });
                } else {
                    return ZFT.Util.STip("fail", res.msg, 4000);
                }
            }
        })
    },
    //删除消息模板
    delNotice: function (id) {
        var _this = this;
        var html=Template.delNotice();
        $("#modal").html(html);
        _this.closeModal();
        $("#confDel").on("click", function () {
            ZFT.Util.Ajax("/r/Message_delete", {
                type: "POST",
                dataType: "json",
                params: {
                    id: id
                },
                success: function (res) {
                    if (res.code == 200) {
                        $("#modal").html("");
                        _this.getTplList(1);
                        return ZFT.Util.STip("success", res.msg, 4000);
                    } else if (res.code == 102) {
                        return ZFT.Util.STip("fail", "未登录,请重新登录", 4000, function () {
                            window.location.href = "/index/index/login";
                        });
                    } else {
                        return ZFT.Util.STip("fail", res.msg, 4000);
                    }
                }
            })
        })
    }

})

$(function () {
    new Main();
})